#include "tcp.h"

/* Usage:  */
int initTcp(char *addr,char *port){              

    int socketFd= socket(AF_INET,SOCK_STREAM,0);
    ERROR_CHECK(socketFd,-1,"socket");

    int reuse=1;
    setsockopt(socketFd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(int));

    struct sockaddr_in serverAddr;
    serverAddr.sin_family=AF_INET;
    serverAddr.sin_port=htons(atoi(port));
    inet_pton(AF_INET,addr,(void *)&serverAddr.sin_addr);

    ERROR_CHECK(bind(socketFd,(struct sockaddr*)&serverAddr,sizeof(serverAddr)),-1,"bind");

    ERROR_CHECK(listen(socketFd,10),-1,"listen");

    return 0;
}

int epollAdd(int epollFd,int fd){

    struct epoll_event event;
    memset(&event,0,sizeof(event));
    event.data.fd=fd;
    event.events=EPOLLIN;
    
    ERROR_CHECK(epoll_ctl(epollFd,EPOLL_CTL_ADD,fd,&event),-1,"socketFd");

    return 0;
}
